package com.rabbitmq.learn.work.unfair;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.learn.common.ConnectionUtils;
import com.rabbitmq.learn.common.RabbitMqConstant;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by Lxk on 2019/8/11.
 */
@SuppressWarnings("Duplicates")
public class Sender {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection con = ConnectionUtils.getConnection();

        Channel channel = con.createChannel();

        /**
         * 每个消费者发送确认消息之前，消息队列不发送下一条消息给消费者，
         * 限制每次发送同一个消费者不超过一条消息
         */
        int perfetchCount = 1;
        channel.basicQos(perfetchCount);

        boolean durable = false;
        channel.queueDeclare(RabbitMqConstant.Work.QUEUE_NAME,durable,false,false,null);

        for(int i = 0;i < 66; i++){
            String msg = "hello work " + i;
            channel.basicPublish("",RabbitMqConstant.Work.QUEUE_NAME,null,msg.getBytes());
            System.out.println(msg + "send success");
            Thread.sleep(25);
        }

        channel.close();

        con.close();

    }

}
